package com.nit.lab.utils;

import com.nit.lab.dto.DtoSysFile;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by be_yond on 2017/10/31.
 */
public class UploadUtie {

    public static void insertData(DtoSysFile dtoSysFile) throws Exception {
        Connection conn = DBConn.getConn();
        conn.setAutoCommit(false);
        String sql = "insert into sys_file(fileName,  fileUrl,  keyWord,  fileFormat,  isPublic,  price,  browseCount,  downloadCount,  uploadTime,fileBelong,userId) values(?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement pstmt;
        pstmt = (PreparedStatement) conn.prepareStatement(sql);
        pstmt.setString(1, dtoSysFile.getFileName());
        pstmt.setString(2, dtoSysFile.getFileUrl());
        pstmt.setString(3, dtoSysFile.getKeyWord());
        pstmt.setInt(4, dtoSysFile.getFileFormat());
        pstmt.setInt(5, dtoSysFile.getIsPublic());
        pstmt.setDouble(6, dtoSysFile.getPrice());
        pstmt.setInt(7, dtoSysFile.getBrowseCount());
        pstmt.setInt(8, dtoSysFile.getDownloadCount());
        pstmt.setString(9, dtoSysFile.getUploadTime());
        pstmt.setInt(10, dtoSysFile.getFileBelong());
        pstmt.setInt(11, dtoSysFile.getUserId());
        pstmt.executeUpdate();
        conn.commit();
        pstmt.close();
    }

    public static String getDate() {
        java.util.Date date = new java.util.Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Odate = sdf.format(date);
        return Odate;
    }

    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

}
